﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SZSLSystem.Web.Admin.Manage
{
    public partial class Edit : SZSLSystem.Web.UI.ManagePage
    {
        #region 全局变量

        #endregion 

        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Request.Params["ID"]))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }
            if (!Page.IsPostBack)
            {
                Guid id = Guid.Parse(Request.Params["ID"]);
                RoleBind(this.chkRole);
                OrganizationBind(this.chkOrganization);
                ShowInfo(id);
            }
        }

       /// <summary>
        /// 赋值操作
       /// </summary>
       /// <param name="id">编号</param>
        private void ShowInfo(Guid id)
        {
            SZSLSystem.BLL.Administrator bll = new SZSLSystem.BLL.Administrator();
            SZSLSystem.Model.Administrator model = bll.GetModel(id);
            this.txtUserName.Text = model.UserName;
            if (model.Status == 1)
            {
                this.rblIsLock.Items[1].Selected = true;
            }
            else
            {
                this.rblIsLock.Items[0].Selected = true;
            }
            txtReadName.Text = model.RealName;
            txtUserEmail.Text = model.Email;
            txtMobile.Text = model.Mobile;
            txtTelphone.Text = model.Telphone;
            txtQQ.Text = model.QQ;

            SZSLSystem.BLL.UserRole aubll = new BLL.UserRole();
            DataTable tb = aubll.GetList("UserID='" + id + "'").Tables[0];
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                this.chkRole.Items.FindByValue(tb.Rows[i]["RoleID"].ToString()).Selected = true;
            }

            SZSLSystem.BLL.OrganizationUser orgbll = new BLL.OrganizationUser();
            DataTable orgtb = orgbll.GetList("UserID='" + id + "'").Tables[0];
            for (int i = 0; i < orgtb.Rows.Count; i++)
            {
                this.chkOrganization.Items.FindByValue(orgtb.Rows[i]["OrganizationID"].ToString()).Selected = true;
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Guid id = Guid.Parse(Request.Params["id"]);
                SZSLSystem.BLL.Administrator bll = new SZSLSystem.BLL.Administrator();
                SZSLSystem.Model.Administrator model = bll.GetModel(id);
                string UserPwd = txtUserPwd.Text.Trim();
                if (!string.IsNullOrEmpty(UserPwd))
                {
                    model.UserPassword = UserPwd;
                }
                else
                {
                    model.UserPassword = "";
                }
                model.RealName = txtReadName.Text.Trim();
                model.Email = txtUserEmail.Text.Trim();
                model.Mobile = txtMobile.Text.Trim();
                model.Telphone = txtTelphone.Text.Trim();
                model.QQ = txtQQ.Text.Trim();
                model.Status = Convert.ToInt32(rblIsLock.SelectedValue);
                bll.Update(model);

                SZSLSystem.BLL.UserRole userbll = new BLL.UserRole();
                userbll.DeletebyUserID(model.ID);
                for (int i = 0; i < this.chkRole.Items.Count; i++)
                {
                    if (this.chkRole.Items[i].Selected)
                    {
                        SZSLSystem.Model.UserRole usermodel = new Model.UserRole();
                        usermodel.ID = Guid.NewGuid();
                        usermodel.UserID = model.ID;
                        usermodel.RoleID = Guid.Parse(this.chkRole.Items[i].Value.ToString());
                        userbll.Add(usermodel);
                    }
                }

                SZSLSystem.BLL.OrganizationUser orgbll = new BLL.OrganizationUser();
                orgbll.DeletebyUserID(model.ID);
                for (int i = 0; i < this.chkOrganization.Items.Count; i++)
                {
                    if (this.chkOrganization.Items[i].Selected)
                    {
                        SZSLSystem.Model.OrganizationUser orgmodel = new Model.OrganizationUser();
                        orgmodel.ID = Guid.NewGuid();
                        orgmodel.UserID = model.ID;
                        orgmodel.OrganizationID = Guid.Parse(this.chkOrganization.Items[i].Value.ToString());
                        orgmodel.AddDate = DateTime.Now;
                        orgbll.Add(orgmodel);
                    }
                }

                //
                //日志写入
                //
                SaveLogs("修改系统用户信息成功！", 0);

                JscriptPrint("修改系统用户信息成功啦！", "List.aspx", "Success");
            }
            catch (Exception err)
            {
                //
                //异常日志写入
                //
                SaveLogs(err.ToString(), 1);

                JscriptMsg(350, 230, "错误提示", "<b>出现错误了！</b>修改组系统用户信息失败！", "", "Error");
            }
        }
    }
}
